package com.bsj.power.sdk.service.handle;

import com.alibaba.fastjson.JSON;
import com.bsj.consumerQ.function.HandleRun;
import com.bsj.consumerQ.function.HandleRunT;
import com.bsj.consumerQ.method.QueueAndRun;
import com.bsj.consumerQ.method.QueueAndRunT;
import com.bsj.power.common.config.cached.PathCached;
import com.bsj.power.common.config.rocketmq.producer.BaseMqPushUtil;
import com.bsj.power.common.constant.AlarmEnum;
import com.bsj.power.common.constant.MqTopicAndTagConstants;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.entity.alarmReport.AlarmReport;
import com.bsj.power.common.def.vo.WebSocketMsgVO;
import com.bsj.power.common.mapper.AlarmReportMapper;
import com.bsj.power.common.util.ExceptionUtil;
import com.bsj.power.sdk.service.BasisStart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description SDK报警信息处理
 * @date 2024/8/28
 */
@Slf4j
@Component
public class HKSDKAlarmHandle implements BasisStart {

    private QueueAndRunT sdkAlarmQueue;

    @Autowired
    private BaseMqPushUtil baseMqPushUtil;
    @Autowired
    private PathCached pathCached;

    @Resource
    private AlarmReportMapper alarmReportMapper;

    @Override
    public void init() {
        //启动消费队列
        sdkAlarmQueue = new QueueAndRunT(5, 200, 20);
        sdkAlarmQueue.start("HKSDKAlarm", new HandleRunT<AlarmReport>() {
            @Override
            public void doRun(List<AlarmReport> list) {
                if (list.isEmpty()) {
                    return;
                }
                try {
                    List<AlarmReport> insertAlarmList = new ArrayList<>();
                    for (AlarmReport alarmReport : list) {
                        Integer alarmType = alarmReport.getAlarmType();
                        Integer alarmLevel = pathCached.hgetAlarmLevel(alarmType);
                        alarmReport.setAlertLevel(alarmLevel);
                        insertAlarmList.add(alarmReport);
                        //报警提醒
                        WebSocketMsgVO webSocketMsgVO = WebSocketMsgVO.builder()
                                .msgType(NumericalConstants.THREE)
                                .obj(alarmReport)
                                .build();
                        baseMqPushUtil.sendTopicBytes(MqTopicAndTagConstants.SYSTEM_WEB_SERVER_CHANGE_TOPIC,
                                MqTopicAndTagConstants.SYSTEM_WEB_PRODUCT_CHANGE_TAG, JSON.toJSONBytes(webSocketMsgVO));
                        //发送消息通知组合报警
                        baseMqPushUtil.sendTopicBytes(MqTopicAndTagConstants.BASE_SERVER_PLS_CHANGE_TOPIC,
                                MqTopicAndTagConstants.BASE_CHANGE_PLS_UPDATE_TAG, JSON.toJSONBytes(alarmReport));
                        if (alarmType.equals(AlarmEnum.COMM_ALARM_V40_BLOCK_OUT.getCode())) {
                            //如果是视频遮挡，则自动再加上丢失视频侦测报警跟视频模糊报警
                            //视频丢失
                            AlarmReport lossAlarm = new AlarmReport();
                            BeanUtils.copyProperties(alarmReport, lossAlarm);
                            lossAlarm.setAlarmType(AlarmEnum.COMM_ALARM_V40_BLOCK_LOSS.getCode());
                            //视频模糊
                            AlarmReport vagueAlarm = new AlarmReport();
                            BeanUtils.copyProperties(alarmReport, vagueAlarm);
                            vagueAlarm.setAlarmType(AlarmEnum.COMM_ALARM_V40_BLOCK_VAGUE.getCode());
                            insertAlarmList.add(lossAlarm);
                            insertAlarmList.add(vagueAlarm);
                        }
                    }
                    //批量插入报警信息
                    alarmReportMapper.addAll(insertAlarmList);
                } catch (Exception e) {
                    log.error("SDK报警信息处理异常: {}", ExceptionUtil.getStackStr(e));
                    return;
                }
            }
        });
    }

    @Override
    public void stop() {
        if (sdkAlarmQueue != null) {
            sdkAlarmQueue.stop();
        }
    }

    @Override
    public void doPush(byte[] bytes) {

    }

    @Override
    public void doPushObj(Object obj) throws Exception{
        if (sdkAlarmQueue == null) {
            log.error("HKSDKAlarmHandle 未初始化");
            init();
        }
        sdkAlarmQueue.push(obj);
    }
}
